home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CDictionary 1.0 / CDictTestDoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  7.7 KB  |  289 lines  |  [TEXT/KAHL]

  1. /****
  2.  * CDictTestDoc.c
  3.  *
  4.  *    Document methods for a typical application.
  5.  *
  6.  ****/
  7.  
  8. #include <Global.h>
  9. #include <Commands.h>
  10. #include <CApplication.h>
  11. #include <CBartender.h>
  12. #include <CDataFile.h>
  13. #include <CDecorator.h>
  14. #include <CDesktop.h>
  15. #include <CError.h>
  16. #include <CPanorama.h>
  17. #include <CScrollPane.h>
  18. #include "CDictTestDoc.h"
  19. #include "CStaticInteger.h"
  20. #include "CSmartWindow.h"
  21. #include "CSmartButton.h"
  22. #include "CSmartEditText.h"
  23. #include "DictTest.h"
  24. #include "CPascalString.h"
  25. #include "CTestDict.h"
  26. #include "CPattern.h"
  27. #include "CFilledBorder.h"
  28. #include "TBUtilities.h"
  29.  
  30. #define    WINDDictTest        500        /* Resource ID for WIND template */
  31.  
  32. extern    CApplication *gApplication;    /* The application */
  33. extern    CBartender    *gBartender;    /* The menu handling object */
  34. extern    CDecorator    *gDecorator;    /* Window dressing object    */
  35. extern    CDesktop    *gDesktop;        /* The enclosure for all windows */
  36. extern    CBureaucrat    *gGopher;        /* The current boss in the chain of command */
  37. extern    OSType        gSignature;        /* The application's signature */
  38. extern    CError        *gError;        /* The global error handler */
  39.  
  40. /*****************************************************************************/
  41. void CDictTestDoc::IDictTestDoc(CBureaucrat *aSupervisor, Boolean printable)
  42.  
  43. {
  44.     CSmartDocument::ISmartDocument(aSupervisor, printable, TRUE);
  45.     
  46.     utilString = new( CPascalString);
  47. }
  48. /*****************************************************************************/
  49. void CDictTestDoc::NewFile(void)
  50. {    
  51.     itsDictionary = new( CTestDict);
  52.     CheckAllocation( itsDictionary);
  53.     itsDictionary->ITestDict();
  54.     BuildWindow();    
  55.     itsWindow->Select();
  56.     
  57. }
  58. /*****************************************************************************/
  59. void CDictTestDoc::Dispose()
  60. {
  61.     if (utilString) utilString->Dispose();
  62.     if (itsDictionary) itsDictionary->DisposeAll();
  63.     inherited::Dispose();
  64. }
  65. /*****************************************************************************/
  66. void CDictTestDoc::DoCommand(long theCommand)
  67.  
  68. {
  69.     switch (theCommand)
  70.     {
  71.         case cmdAddAssoc:
  72.             AddAssociation();
  73.             break;
  74.             
  75.         case cmdRemoveAssoc:
  76.             RemoveAssociation();
  77.             break;    
  78.     
  79.         case cmdLookupAssoc:
  80.             LookupAssociation();
  81.             break;
  82.             
  83.         case cmdAddLots:
  84.             AddLots();
  85.             break;
  86.  
  87.         default:
  88.             inherited::DoCommand(theCommand);
  89.             break;
  90.     }
  91. }
  92. /*****************************************************************************/
  93. void CDictTestDoc::BuildWindow ( void)
  94. {
  95.     CScrollPane        *theScrollPane;
  96.     CPanorama        *theMainPane;
  97.     CFilledBorder            *border;
  98.     Rect            r;
  99.     CStaticString    *string;
  100.     CSmartWindow    *window;
  101.     CSmartButton    *btn;
  102.     CPattern        *pat;
  103.  
  104.     window = new(CSmartWindow);
  105.     CheckAllocation( window);
  106.     window->ISmartWindow( WINDDictTest, FALSE, gDesktop, this, TRUE);
  107.     itsWindow = window;
  108.     
  109.     theScrollPane = new(CScrollPane);
  110.     CheckAllocation( theScrollPane);
  111.     theScrollPane->IViewRes('ScPn', 1, itsWindow, this);
  112.     theScrollPane->FitToEnclFrame(TRUE, TRUE);
  113.  
  114.     theMainPane = new( CPanorama);
  115.     CheckAllocation( theMainPane);
  116.     itsMainPane = theMainPane;
  117.  
  118.     theMainPane->IPanorama( theScrollPane, this, 0, 0, 0, 0, sizELASTIC, sizELASTIC);
  119.     theMainPane->FitToEnclosure( TRUE, TRUE);
  120.     theMainPane->SetWantsClicks( TRUE);
  121.     theMainPane->GetFrame( &r);
  122.     theMainPane->SetBounds( &r);
  123.     theScrollPane->InstallPanorama( theMainPane);
  124.     
  125.     pat = new(CPattern);
  126.     CheckAllocation( pat);
  127.     pat->IPattern( theMainPane, this, 0, 0, 0, 0,
  128.                 sizELASTIC, sizELASTIC, 300, TRUE);
  129.     pat->FitToEnclosure( TRUE, TRUE);
  130.             
  131.     string = new( CStaticString);
  132.     CheckAllocation( string);
  133.     string->IViewRes( 'StSt', 1, theMainPane, itsSupervisor);
  134.     string = new( CStaticString);
  135.     CheckAllocation( string);
  136.     string->IViewRes( 'StSt', 2, theMainPane, itsSupervisor);
  137.     string = new( CStaticString);
  138.     CheckAllocation( string);
  139.     string->IViewRes( 'StSt', 3, theMainPane, itsSupervisor);
  140.     itsItemCount = new( CStaticInteger);
  141.     CheckAllocation( itsItemCount);
  142.     itsItemCount->IViewRes( 'StSt', 4, theMainPane, itsSupervisor);
  143.     itsItemCount->SetIntValue( itsDictionary->GetNumItems());
  144.     
  145.     border = new(CFilledBorder);
  146.     CheckAllocation( border);
  147.     border->IFilledBorder( theMainPane, this, 100, 30, 50, 10, sizFIXEDSTICKY,
  148.                 sizFIXEDSTICKY);
  149.     border->SetWantsClicks( TRUE);
  150.  
  151.     itsKeyText = new( CSmartEditText);
  152.     CheckAllocation( itsKeyText);
  153.     itsKeyText->ISmartEditText( border, this, 0, 0, 0, 0, sizELASTIC,
  154.                 sizELASTIC, -1, 1);
  155.     itsKeyText->FitToEnclosure( TRUE, TRUE);
  156.     SetRect( &r, 2, 2, -2, -2);
  157.     itsKeyText->ChangeSize( &r, TRUE);                
  158.     
  159.     border = new(CFilledBorder);
  160.     CheckAllocation( border);
  161.     border->IFilledBorder( theMainPane, this, 200, 100, 50, 50, sizFIXEDSTICKY,
  162.                 sizFIXEDSTICKY);
  163.     border->SetWantsClicks( TRUE);
  164.     
  165.     itsValueText = new( CSmartEditText);
  166.     CheckAllocation( itsValueText);
  167.     itsValueText->ISmartEditText( border, this, 0, 0, 0, 0, sizELASTIC,
  168.                 sizELASTIC, -1, 1);
  169.     itsValueText->FitToEnclosure( TRUE, TRUE);
  170.     SetRect( &r, 2, 2, -2, -2);
  171.     itsValueText->ChangeSize( &r, TRUE);
  172.     
  173.     ActivateEditItem( itsKeyText);
  174.     
  175.     btn = new( CSmartButton);
  176.     CheckAllocation( btn);
  177.     btn->ISmartButton( 1, theMainPane, this);    /* Add */
  178.     btn = new( CSmartButton);
  179.     CheckAllocation( btn);
  180.     btn->ISmartButton( 2, theMainPane, this);    /* Remove */
  181.     btn = new( CSmartButton);
  182.     CheckAllocation( btn);
  183.     btn->ISmartButton( 3, theMainPane, this);    /* Lookup */
  184.  
  185.     gDecorator->PlaceNewWindow( itsWindow);
  186. }
  187. /*****************************************************************************/
  188. void CDictTestDoc::AddAssociation( void)
  189. {
  190.     Str255    key;
  191.     Str255    valueStr;
  192.     CPascalString    *value;
  193.     
  194.     itsKeyText->GetString( key);
  195.     key[0] = MIN( key[0], 20);
  196.     
  197.     itsValueText->GetString( valueStr);
  198.     value = new( CPascalString);
  199.     CheckAllocation( CPascalString);
  200.     
  201.     value->SetString( valueStr);
  202.     
  203.     itsDictionary->Add( key, value);
  204.     itsItemCount->SetIntValue( itsDictionary->GetNumItems());
  205.     dirty = TRUE;
  206.     
  207. }    /* CDictTestDoc::AddAssociation */
  208. /*****************************************************************************/
  209. void CDictTestDoc::RemoveAssociation( void)
  210. {
  211.     Str255    key;
  212.     
  213.     itsKeyText->GetString( key);
  214.     key[0] = MIN( key[0], 20);
  215.     
  216.     itsDictionary->Remove( key);
  217.     itsValueText->ClearText();
  218.     itsItemCount->SetIntValue( itsDictionary->GetNumItems());
  219.     dirty = TRUE;
  220.  
  221. }    /* CDictTestDoc::RemoveAssociation */
  222. /*****************************************************************************/
  223. void CDictTestDoc::LookupAssociation( void)
  224. {
  225.     Str255    key;
  226.     Str255    valueStr;
  227.     CPascalString    *value;
  228.     
  229.     itsKeyText->GetString( key);
  230.     key[0] = MIN( key[0], 20);
  231.     
  232.     value = (CPascalString*) itsDictionary->Lookup( key);
  233.     if (value)
  234.         value->GetString(valueStr);
  235.     else
  236.     {
  237.         valueStr[0] = 0;
  238.         SysBeep(3);
  239.     }
  240.     itsValueText->SetTextString( valueStr);
  241.  
  242. }    /* CDictTestDoc::LookupAssociation */
  243. /*****************************************************************************/
  244. void CDictTestDoc::AddLots( void)
  245. {
  246.     Str255    s, s2;
  247.     Int32    i;
  248.     #define kNumTestEntries    250
  249.     
  250.     for( i = 1; i <= kNumTestEntries; i++)
  251.     {
  252.         NumToString( i+1000, s);
  253.         itsKeyText->SetTextString(s);
  254.         pStrCopy( "\pentry ", s2);
  255.         pConcat( s2, s);
  256.         itsValueText->SetTextString(s2);
  257.         AddAssociation();
  258.         if ((i % 50) == 0)
  259.             itsWindow->Update();
  260.             
  261.         if (MaxBlock() < 32767L) return;
  262.     }
  263.  
  264. }    /* CDictTestDoc::AddLots */
  265. /*****************************************************************************/
  266. void CDictTestDoc::UpdateMenus( void)
  267. {
  268.     inherited::UpdateMenus();
  269.     
  270.     gBartender->EnableCmd( cmdAddAssoc);
  271.     gBartender->EnableCmd( cmdLookupAssoc);
  272.     gBartender->EnableCmd( cmdRemoveAssoc);
  273.     gBartender->EnableCmd( cmdAddLots);
  274.     
  275.     gBartender->DisableCmd( cmdSave);
  276.     gBartender->DisableCmd( cmdSaveAs);
  277.     gBartender->DisableCmd( cmdRevert);
  278.  
  279.  
  280. }    /* CDictTestDoc::UpdateMenus */
  281. /*****************************************************************************/    
  282. Boolean    CDictTestDoc::ConfirmClose(Boolean quitting)
  283. {
  284.     return TRUE; /* never save the document */
  285.  
  286. }    /* CDictTestDoc::UpdateMenus */
  287. /*****************************************************************************/    
  288.  
  289.